package com.hongye.house.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.Query;
import com.hongye.house.config.HongYeESConfig;
import com.hongye.house.dao.RequirementDao;
import com.hongye.house.entity.RequirementEntity;
import com.hongye.house.service.RequirementService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Service("requirementService")
public class RequirementServiceImpl extends ServiceImpl<RequirementDao, RequirementEntity> implements RequirementService {



    @Autowired
    private RestHighLevelClient client;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RequirementEntity> page = this.page(
                new Query<RequirementEntity>().getPage(params),
                new QueryWrapper<RequirementEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void esrentinginsert() {
        List<RequirementEntity> list = baseMapper.selectList(new QueryWrapper<RequirementEntity>().eq("requirement_type", 1));
        //批量请求
        BulkRequest request = new BulkRequest();
        list.stream().forEach( a ->{
            //创建索引
            IndexRequest indexRequest = new IndexRequest("rentingshop").id(a.getRequirementId().toString());
            //因为我的a是个对象 循环查每一条数据 批量添加
            //es里添加规定的要string类型所以要把a 转成string类型进行操作
            String s = JSONObject.toJSONString(a);

            IndexRequest source = indexRequest.source(s, XContentType.JSON);
            //添加
            request.add(indexRequest,source);
        });
        try {
            client.bulk(request, HongYeESConfig.COMMON_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void esbuyhouseinsert() {
        List<RequirementEntity> list = baseMapper.selectList(new QueryWrapper<RequirementEntity>().eq("requirement_type", 2));
        //批量请求
        BulkRequest request = new BulkRequest();
        list.stream().forEach( a ->{
            //创建索引
            IndexRequest indexRequest = new IndexRequest("buyhouseshop").id(a.getRequirementId().toString());
            //因为我的a是个对象 循环查每一条数据 批量添加
            //es里添加规定的要string类型所以要把a 转成string类型进行操作
            String s = JSONObject.toJSONString(a);

            IndexRequest source = indexRequest.source(s, XContentType.JSON);
            //添加
            request.add(indexRequest,source);
        });
        try {
            client.bulk(request, HongYeESConfig.COMMON_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}